package com.stationpc.dock.activity

import android.content.Intent
import android.os.Bundle
import android.widget.GridLayout
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.stationpc.dock.AppInfo
import com.stationpc.dock.R
import com.stationpc.dock.adapter.AppAdapter
import android.provider.Settings
import android.net.Uri
import android.util.Log

class DockLayoutSetupActivity : AppCompatActivity() {

    private lateinit var gridLayout: GridLayout
    private lateinit var appAdapter: AppAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_dock_layout_setup)

        gridLayout = findViewById(R.id.dockGridLayout)

        // Initialize AppAdapter with click and long-click actions
        appAdapter = AppAdapter(
            onItemClick = { appInfo ->
                // Click handler: open custom app info activity
                openAppInfoActivity(appInfo)
            },
            onItemLongClick = { appInfo ->
                // Long-click handler: open app info in system settings
                openAppInfo(appInfo.packageName)
            }
        )

        // Load the apps into the GridLayout
        loadApps()
    }

    private fun loadApps() {
        gridLayout.removeAllViews()

        val packageManager = packageManager
        val intent = Intent(Intent.ACTION_MAIN, null).apply {
            addCategory(Intent.CATEGORY_LAUNCHER)
        }

        val apps = packageManager.queryIntentActivities(intent, 0)
        val appInfos = apps.map {
            AppInfo(it.activityInfo.packageName, it.loadLabel(packageManager).toString(), it.loadIcon(packageManager))
        }

        // Add apps to the GridLayout
        for (appInfo in appInfos) {
            val appIconView = layoutInflater.inflate(R.layout.dock_app_item, gridLayout, false)
            val iconView = appIconView.findViewById<ImageView>(R.id.appIcon)
            iconView.setImageDrawable(appInfo.icon)

            // The adapter will handle clicks
            appAdapter.onBindViewHolder(
                AppAdapter.AppViewHolder(appIconView, { app ->
                    openAppInfoActivity(app)
                }, { app ->
                    openAppInfo(app.packageName)
                }),
                appInfos.indexOf(appInfo)
            )

            gridLayout.addView(appIconView)
        }
    }

    private fun openAppInfo(packageName: String) {
        try {
            val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
                data = Uri.parse("package:$packageName")
                addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            }
            startActivity(intent)
        } catch (e: Exception) {
            Log.e("DockLayoutSetupActivity", "Unable to open app info: $packageName", e)
        }
    }

    private fun openAppInfoActivity(appInfo: AppInfo) {
        val intent = Intent(this, AppInfoActivity::class.java).apply {
            putExtra("appInfo", appInfo)
        }
        startActivity(intent)
    }
}
